package com.mobisys.live.controller;

import com.mobisys.live.dao.mapper.InfoEnterpriseMapper;
import com.mobisys.live.dao.mapper.InfoPersonalMapper;
import com.mobisys.live.dao.mapper.PictureMapper;
import com.mobisys.live.dao.mapper.UserMapper;
import com.mobisys.live.dao.model.InfoEnterprise;
import com.mobisys.live.dao.model.InfoPersonal;
import com.mobisys.live.dao.model.Picture;
import com.mobisys.live.response.Response;
import com.mobisys.live.response.identify.InfoPersonalResponse;
import com.mobisys.live.util.PictureUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.mobisys.live.config.ResponseCode.*;

/**
 * Created by liu
 * 个人和企业的认证
 */

@RestController
public class IdentificationController {
    //个人认证
    @Autowired
    private InfoPersonalMapper infoPersonalMapper;
    //企业认证
    @Autowired
    private InfoEnterpriseMapper infoEnterpriseMapper;

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PictureMapper pictureMapper;

    private Logger logger = LogManager.getLogger(IdentificationController.class.getName());

    /*
    * 收取个人认证的信息
    *
    * */
    @RequestMapping(value = "/1.0/identify/infopersonal/submit", method = RequestMethod.POST)
    public Response subInfoPerson(@RequestBody InfoPersonalResponse infoPersonalResponse){
        Response response = new Response();

        if(infoPersonalResponse == null){
            response.setRetureValue(INFOPERSONAL_IS_ERROR);
            response.setRetureMsg(MSG_INFOPERSONAL_IS_ERROR);
            return response;
        }
        //将前端提交的数据充入个人认证表的类中
        InfoPersonal infoPersonal = new InfoPersonal();

        infoPersonal.setLocation(infoPersonalResponse.getLocation());
        infoPersonal.setAddress(infoPersonalResponse.getAddress());
        infoPersonal.setRealName(infoPersonalResponse.getRealName());
        infoPersonal.setIdNumber(infoPersonalResponse.getIdNumber());
        infoPersonal.setIdCardPicId(PictureUtil.saveImgAndGetPicureId(infoPersonalResponse.getIdCardPicId(),pictureMapper));
        infoPersonal.setIsReview(0);// 0代表未审核 1代表审核通过

        int n = infoPersonalMapper.insert(infoPersonal);

        if(n == 0){
            logger.debug("infoPersonal插入失败！");
            response.setRetureValue(INFOPERSONAL_IS_ERROR);
            response.setRetureMsg(MSG_INFOPERSONAL_IS_ERROR);
            return response;
        }
        //在user表中加入关联
        int m = userMapper.updateByUsername(
                infoPersonalResponse.getUsername(),infoPersonal.getId());

        if(m == 0){
            logger.debug("infoPersonal和user关联失败！");
            response.setRetureValue(INFOPERSONAL_AND_USER_ERROR);
            response.setRetureMsg(MSG_INFOPERSONAL_AND_USER_ERROR);
            return response;
        }
        response.setRetureValue(SUCCESS);
        response.setRetureMsg(MSG_SUCCESS);
        return response;
    }

    /*
    * 个人信息待审核信息筛查
    * */
    @RequestMapping(value = "/1.0/identify/infopersonal/search")
    public Response getInfopersonal (){
        Response response = new Response();
         List<InfoPersonal> infoPersonalList = infoPersonalMapper.selectByIsReview(0);

         if(infoPersonalList.isEmpty()){
             response.setRetureValue(INFOPERSONAL_IS_ERROR);
             response.setRetureMsg(MSG_INFOPERSONAL_IS_ERROR);
             return response;
         }
        response.setRetureValue(SUCCESS);
        response.setRetureMsg(MSG_SUCCESS);
        response.setRetureData(infoPersonalList);
        return response;
    }
    /*
   * 个人认证审核
   * */
    @RequestMapping(value = "/1.0/identify/infopersonal/ischeck")
    public Response identifyInfopersonal (@RequestParam String id){
        Response response = new Response();
        InfoPersonal infoPersonal = new InfoPersonal();
        infoPersonal.setId(id);
        infoPersonal.setIsReview(1);//审核通过

        int i= infoPersonalMapper.updateByPrimaryKey(infoPersonal);
        if(i == 0){
            logger.debug("infoPersonal和user关联失败！");
            response.setRetureValue(INFOPERSONAL_IS_ERROR);
            response.setRetureMsg(MSG_INFOPERSONAL_IS_ERROR);
            return response;
        }
        response.setRetureValue(SUCCESS);
        response.setRetureMsg(MSG_SUCCESS);
        return response;

    }




}
